<?php
	class Loaigiuong_model extends CI_Model
	{
		function LayTatCaLoaiGiuong()
		{
			$post_per_page = 20;
			
			$this->db->select('*');
			$this->db->from('LoaiGiuong');
			$this->db->limit($post_per_page, $this->uri->segment(3));
			$query = $this->db->get();

			$this->load->library('pagination');
			$config['base_url'] = base_url().'index.php/loaigiuong/admin_index';
			$config['total_rows'] = $this->db->get('LoaiGiuong')->num_rows();			
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
				
		function Tim($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(4))
				$num = $this->uri->segment(4);			
			
			$this->db->select('*');
			$this->db->from('LoaiGiuong');
			$this->db->like('MaLG', $key, 'none');
			$this->db->or_like('Ten', $key);
			$this->db->limit($post_per_page, $num);
			$query = $this->db->get();
			
			$this->db->select('*');
			$this->db->from('LoaiGiuong');
			$this->db->like('MaLG', $key, 'none');
			$this->db->or_like('Ten', $key);
			$total_query = $this->db->get();			

			$this->load->library('pagination');
			$config['uri_segment'] = 4;
			$config['base_url'] = base_url().'index.php/loaigiuong/Tim/'.$key;
			$config['total_rows'] = $total_query->num_rows();			
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function LayMaLG_ajax($key)
		{
			$this->db->select('LoaiGiuong.MaLG AS Ten');
			$this->db->from('LoaiGiuong');
			$this->db->like('MaLG', $key);
			$this->db->order_by('MaLG', "asc");
			$query = $this->db->get();
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function LayTenLG_ajax($key)
		{
			$this->db->select('Ten');
			$this->db->from('LoaiGiuong');
			$this->db->like('Ten', $key);
			$this->db->order_by('Ten', "asc");
			$query = $this->db->get();
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}		
		
		function LayLoaiGiuong($malg)
		{
			$this->db->select('*');
			$this->db->from('LoaiGiuong');
			$this->db->where('MaLG', $malg);
			$query = $this->db->get();
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function KiemTraTrungTen($tenlg)
		{
			$strSQL = "SELECT * FROM LoaiGiuong WHERE Ten = '$tenlg'";
			$result = $this->db->query($strSQL)->num_rows();
			if($result > 0)
				return 1;
			return 0;
		}
		
		function LayTatCaLoaiGiuong_KhongPhanTrang()
		{
			$query = $this->db->get('LoaiGiuong');
			
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function Xoa($malg)
		{
			return $this->db->delete('LoaiGiuong', array('MaLG' => $malg));
		}
		
		function Them($data)
		{
			$this->db->insert('LoaiGiuong', $data);
			return $this->db->affected_rows();
		}
				
		function CapNhat($data, $malg)
		{
			$this->db->update('LoaiGiuong', $data, array('MaLG' => $malg)); 
			return $this->db->affected_rows();			
		}	
	}
?>